using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Configuration;
using OpenMTS.Properties;
using NAVA.MTSLibrary;

namespace OpenMTS
{
    public partial class Form1 : Form
    {
        #region PROPERTIES
        private string tempFileName = "";
        #endregion

        #region CONSTRUCTORS
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region METHODS
        private void viewOutput(string sResultXml, bool bViewAsXml, string sReportPath)
        {
            if(tempFileName.Length > 0)
                File.Delete(tempFileName);

            if (bViewAsXml)
            {
                tempFileName = Path.GetTempFileName();
                tempFileName = tempFileName + ".xml";

                System.IO.StreamWriter sw = System.IO.File.CreateText(tempFileName);
                sw.Write(sResultXml);
                sw.Close();

                webBrowser1.Navigate(tempFileName);
                
            }
            else
            {

                XmlDocument xsl = new XmlDocument();
                xsl.Load(sReportPath);                    
                XslCompiledTransform xTransform = new XslCompiledTransform();
                xTransform.Load(xsl.CreateNavigator(), null, null);

                XmlDocument xIn = new XmlDocument();
                xIn.LoadXml(sResultXml);
                MemoryStream ms = new MemoryStream();

                StringBuilder sb = new StringBuilder();
                StringWriter sw1 = new StringWriter(sb);
                xTransform.Transform(xIn.CreateNavigator(), null, sw1);

                tempFileName = Path.GetTempFileName();
                tempFileName = tempFileName + ".htm";

                System.IO.StreamWriter sw = System.IO.File.CreateText(tempFileName);
                sw.Write(sb.ToString());
                sw.Close();

                webBrowser1.Navigate(tempFileName);
             
            }
        }

        private XmlDocument loadInternalXmlDocument(string resourceName)
        {
            XmlDocument inXsl = new XmlDocument();
            string xmlResouce = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
               + "." + resourceName;
            using (Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(xmlResouce))
            {
                inXsl.Load(s);
            }

            return inXsl;
        }

        private string buildFileNameWithPath(string sFile, string appSetting)
        {
            string sPath = ConfigurationManager.AppSettings[appSetting];
            if (!sPath.EndsWith("\\"))
            {
                sPath += "\\";
            }

            return sPath + sFile;

        }
      
        #endregion

        #region EVENTS
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowSaveAsDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 a = new AboutBox1();
            a.ShowDialog();
        }

        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RunPreferences r = new RunPreferences();
            DialogResult result = r.ShowDialog();
            if (result == DialogResult.Cancel)
                return;

            try
            {
                this.Cursor = Cursors.WaitCursor;

                MessageTester mt = null;
                mt = MessageTester.MessageTesterFactory();
                XmlDocument xInputMessage = new XmlDocument();
                xInputMessage.Load(Settings.Default.InputFile);

                XmlDocument xRuleset = new XmlDocument();

                xRuleset.Load(buildFileNameWithPath(Settings.Default.RulesetFile,"RulesetLocation"));
                string sOutput;

                sOutput = mt.evaluate(xInputMessage.OuterXml, xRuleset.OuterXml);

                viewOutput(sOutput, Settings.Default.ViewAsXml,
                    buildFileNameWithPath(Settings.Default.ReportTemplate,
                    "ReportLocation"));

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        
         
        private void Form1_Load(object sender, EventArgs e)
        {
            tempFileName = Path.GetTempFileName();
            tempFileName = tempFileName + ".xml";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.Delete(tempFileName);
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            webBrowser1.ShowPrintDialog();
           
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Preferences p = new Preferences();
            p.ShowDialog();
        }

        private void xPathReferenceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open the default browser to the XPath documentation URL
            string sUrl = ConfigurationManager.AppSettings["XPathRef"];
            System.Diagnostics.Process.Start(sUrl);
        }

       

        private void expressionEvaluatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EvaluatorDlg dlg = new EvaluatorDlg();
            dlg.Show(this);
        }

        private void userGuideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sUrl = ConfigurationManager.AppSettings["HelpGuide"];
            
            System.Diagnostics.Process.Start(sUrl);
        }

        #endregion
    }

}